package com.shujia.mr.wc2;

import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.dictionary.CustomDictionary;
import com.hankcs.hanlp.seg.Segment;
import com.hankcs.hanlp.seg.common.Term;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.util.List;

class SgyyMapper extends Mapper<LongWritable, Text, Text, LongWritable> {
    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, LongWritable>.Context context) throws IOException, InterruptedException {
        String line = value.toString();
        Segment segment = HanLP.newSegment();

        //加入词典
        CustomDictionary.add("张飞");
        CustomDictionary.add("张角");
        CustomDictionary.add("玄德");
        List<Term> terms = segment.seg(line);
        for (Term term : terms) {
            String word = term.word;
            if ("张飞".equals(word) || "张角".equals(word) || "玄德".equals(word)) {
                context.write(new Text(word), new LongWritable(1L));
            }
        }

    }
}

class SgyyReducer extends Reducer<Text, LongWritable, Text, LongWritable> {
    @Override
    protected void reduce(Text key, Iterable<LongWritable> values, Reducer<Text, LongWritable, Text, LongWritable>.Context context) throws IOException, InterruptedException {
        long sum = 0L;
        for (LongWritable value : values) {
            sum += value.get();
        }

        context.write(key,new LongWritable(sum));
    }
}

public class SgyyDemo {
    public static void main(String[] args) throws Exception{
        //获取Hadoop环境配置对象
        Configuration conf = new Configuration();

        //设置主节点
        conf.set("fs.defaultFS", "hdfs://master:9000");

        //创建Job作业
        Job job = Job.getInstance(conf);

        job.setJarByClass(SgyyDemo.class);

        //设置当前job作业的名字
        job.setJobName("32期 三国演义案例mapreduce实现");

        //设置当前作业要执行的map类
        job.setMapperClass(SgyyMapper.class);
        //设置当前作业要执行的reduce类
        job.setReducerClass(SgyyReducer.class);

        //设置Map任务输出的键值类型和上面指定的map类输出键值类型一一对应
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);

        //设置Reduce任务输出的键值类型和上面指定的reduce类输出键值类型一一对应
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);

        // 设置数据读取的路径【hdfs上的路径】
        FileInputFormat.setInputPaths(job, new Path(args[0]));
        // 设置数据输出路径
        FileOutputFormat.setOutputPath(job, new Path(args[1]));


        // 提交作业到yarn上
        boolean b = job.waitForCompletion(true);
        if(b){
            System.out.println("32期 三国演义案例mapreduce实现执行成功！>_-");
        }else {
            System.out.println("32期 三国演义案例mapreduce实现执行失败！T_T");
        }
    }
}
